import pygame

from PyPixelCore.Setting import Color_type, collision_surface_width, block_surface_width, show_rect_collision, fps_limit
from PyPixelCore.block.TextButton import TextButton

"""
    SelectButton --> TextButton --> TextBlock --> Block --> BaseBlock
    显示选择的文字按钮
    继承优化的按钮
    功能:
        显示文字
        支持碰撞检测
        支持帧函数与计时器函数
        支持点击事件
        支持鼠标移入移出事件
        不支持自动换行绘制
        支持选择
        支持脏精灵优化
"""


class SelectButton(TextButton):
    def __init__(self, text_content="未定义按钮", point_x=0, point_y=0, width=0, height=0, border_width=2, layer=0,
                 is_alpha=False):
        self.button_color = Color_type.LIGHT_RED
        super().__init__(text_content, point_x, point_y, width, height, border_width, layer, is_alpha)
        self.hover_color = Color_type.DEEP_GREY

    def mouse_hover_enter(self):
        self.now_border_color = self.hover_color
        self.is_dirty = True

    def mouse_hover_leave(self):
        self.now_border_color = self.border_color
        self.is_dirty = True

    def click_down(self, pos):
        self._is_clicked = not self._is_clicked
        self.click_down_func(self)
        self.is_dirty = True

    def click_up(self):
        self.click_up_func(self)
        self.is_dirty = True

    def set_click_down_func(self, func):
        self.click_down_func = func

    def set_click_up_func(self, func):
        self.click_up_func = func

    def set_color_no_None(self, font_color=None, button_color=None, border_color=None, hover_color=None,
                          press_color=None):
        if font_color is not None:
            self.font_color = font_color
        if button_color is not None:
            self.bk_color = button_color
        if border_color is not None:
            self.border_color = border_color
        if hover_color is not None:
            self.hover_color = hover_color
        if press_color is not None:
            self.press_color = press_color
        self.is_dirty = True

    def _reload_image_first(self):
        self.now_border_color = self.border_color
        self.text_surf = self.font.render(self.text_content, True, self.font_color)
        t_width = (self.text_surf.get_size()[0] + 2 * self.border_width
                   if self.width == 0 else self.width + 2 * self.border_width) + self._font_size
        t_height = self.text_surf.get_size()[1] + 2 * self.border_width \
            if self.height == 0 else self.height + 2 * self.border_width
        if self.is_alpha:
            self.image = pygame.Surface((t_width, t_height), pygame.SRCALPHA)
        else:
            self.image = pygame.Surface((t_width, t_height))
        self.image.fill(self.bk_color)
        self.rect.size = self.image.get_size()

        # 绘制选择按钮
        pygame.draw.circle(self.image, self.button_color,
                           ((self.border_width + self._font_size)/2, self.rect.h/2 + 1), self._font_size/2
                           , width=0 if self._is_clicked else 3)
        # 绘制方框到主图上
        if self.border_width != 0:
            pygame.draw.rect(self.image, self.now_border_color,
                         (0, 0, self.rect.width, self.rect.height), width=self.border_width)
        # 绘制文字到主图上
        self.image.blit(self.text_surf,
                        ((self.rect.w + self._font_size - self.text_surf.get_size()[0]) // 2,
                         (self.rect.h - self.text_surf.get_size()[1]) // 2,
                         self.rect.width, self.rect.height))
        self.collision_rect = self.rect

    def reload_image(self):
        if not self.is_dirty:
            return
        self.is_dirty = False

        self.image.fill(self.bk_color)
        # 绘制选择按钮
        pygame.draw.circle(self.image, self.button_color,
                           ((self.border_width + self._font_size)/2, self.rect.h/2 + 1), self._font_size/2
                           , width=0 if self._is_clicked else 3)

        # 绘制文字到主图上
        self.image.blit(self.text_surf,
                        ((self.rect.w + self._font_size - self.text_surf.get_size()[0]) // 2,
                         (self.rect.h - self.text_surf.get_size()[1]) // 2,
                         self.rect.width, self.rect.height))

        # 绘制方框到主图上
        pygame.draw.rect(self.image, self.now_border_color,
                         (0, 0, self.rect.width, self.rect.height), width=self.border_width)

    def reload_text_surf(self):
        self.text_surf = self.font.render(self.text_content, True, self.font_color)
        t_width = (self.text_surf.get_size()[0] + 2 * self.border_width
                   if self.width == 0 else self.width + 2 * self.border_width) + self._font_size
        t_height = self.text_surf.get_size()[1] + 2 * self.border_width \
            if self.height == 0 else self.height + 2 * self.border_width
        if self.is_alpha:
            self.image = pygame.Surface((t_width, t_height), pygame.SRCALPHA)
        else:
            self.image = pygame.Surface((t_width, t_height))
        self.image.fill(self.now_bk_color)
        self.rect.size = self.image.get_size()
        self.collision_rect = self.rect

    def set_text_content(self, text_content):
        self._text_content = text_content
        self.reload_text_surf()
        self.is_dirty = True



def base_draw(self: SelectButton, surf):
    surf.blit(self.image, self.rect)
    if show_rect_collision:
        # pygame.draw.rect(surf, Color_type.RED, self.rect, width=3)
        pygame.draw.rect(surf, Color_type.BLACK, self.collision_rect, width=collision_surface_width)
        pygame.draw.rect(surf, Color_type.RED, self.rect, width=block_surface_width)

        # pygame.draw.circle(surf, Color_type.RED, self.rect.center, 50, width=2)
        # pygame.draw.line(surf, Color_type.RED, self.rect.bottomleft, self.rect.bottomright, width=4)
        # pygame.draw.polygon(surf, Color_type.RED, [(1, 1), (27, 90), (111, 111)], width=3)
        # pygame.draw.arc(surf, Color_type.RED, self.rect, 0, 5, width=100)
        # pygame.draw.aalines(surf, Color_type.RED, False, [(1, 1), (27, 90), (111, 111)], blend=1)


def basic_mouse_down(self: TextButton):
    pass


def basic_mouse_up(self: TextButton):
    pass
